package com.recSystem.Service.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.recSystem.Entity.Converters.AuthorConverter;
import com.recSystem.Entity.DO.*;
import com.recSystem.Entity.DTO.*;
import com.recSystem.Entity.DTO.Param.AuthorInfoParam;
import com.recSystem.Entity.POJO.HttpResponse;
import com.recSystem.Mapper.AuthorMapper;
import com.recSystem.Mapper.InterestMapper;
import com.recSystem.Mapper.PaperMapper;
import com.recSystem.Mapper.WorkMapper;
import com.recSystem.Service.AuthorService;
import com.recSystem.Utils.Constant;
import com.recSystem.Utils.PageParser;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Legolas_PRC
 * @date 2023/4/6 20:55
 **/
@Service
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper, AuthorDO> implements AuthorService {
    private final WorkMapper workMapper;
    private final InterestMapper interestMapper;
    private final AuthorMapper authorMapper;
    private final PaperMapper paperMapper;

    public AuthorServiceImpl(WorkMapper workMapper, InterestMapper interestMapper, AuthorMapper authorMapper, PaperMapper paperMapper) {
        this.workMapper = workMapper;
        this.interestMapper = interestMapper;
        this.authorMapper = authorMapper;
        this.paperMapper = paperMapper;
    }

    /**
     * 根据论文id获得该论文的全部作者集合
     *
     * @param paperId
     * @return
     */
    @Override
    public List<AuthorDTO> getAuthorsByPaperId(int paperId) {
        // 要根据论文id得到作者DTO集合
        // 先根据论文id得到创作关系集合
        List<WorkDO> workRelationList = workMapper.selectList(new QueryWrapper<WorkDO>().eq("paper_id", paperId));
        // 根据创作关系得到作者集合
        List<AuthorDO> authors = workRelationList.stream()
                .map(workRelation -> authorMapper.selectById(workRelation.getAuthorId()))
                .collect(Collectors.toList());
        // 把authorDO映射成AuthorDTO
        List<AuthorDTO> authorDTOs = authors.stream().map(authorDO -> {
            AuthorConverter instance = AuthorConverter.INSTANCE;
            return instance.authorDoToDto(authorDO);
        }).collect(Collectors.toList());

        return authorDTOs;
    }

    @Override
    public HttpResponse<AuthorNetDTO> getAuthorNet(AuthorInfoParam param, String token) {
        // 根据作者id查找其发表的论文，通过论文id获取作者信息，即获取到作者合作网络
        int authorId = param.getAuthorId();
        QueryWrapper<WorkDO> qw = new QueryWrapper<>();
        List<WorkDO> workDOS = workMapper.selectList(qw.eq("author_id", authorId));
        List<PaperByAuthorDTO> paperList = new ArrayList<>();
        for (int i = 0; i < workDOS.size(); i++) {
            WorkDO workDO = workDOS.get(i);
            int paperId = workDO.getPaperId();
            PaperDO paperDO = paperMapper.selectById(paperId);
            List<AuthorDTO> authorList = getAuthorsByPaperId(paperId);
            PaperByAuthorDTO paper = new PaperByAuthorDTO(paperDO.getPaperId(), paperDO.getTitle(), authorList, paperDO.getUpdateDate());
            paperList.add(paper);
        }
        AuthorNetDTO authorNetDTO = new AuthorNetDTO();
        authorNetDTO.setPaperList(paperList);
        return new HttpResponse<>(new StatusDTO(0, "获取作者合作网络成功"), authorNetDTO);
    }

    @Override
    public IPage<AuthorInterestDTO> getInterestAuthorsPage(int userId, int maxLength, int offset) {
        // 分页信息
        IPage<InterestDO> pageInfo = PageParser.getPage(maxLength, offset);
        QueryWrapper<InterestDO> interestDOQueryWrapper = new QueryWrapper<>();
        // 得到关注关系集合
        IPage<InterestDO> interestRelationsPage = interestMapper.
                selectPage(pageInfo, interestDOQueryWrapper.eq("user_id", userId));

        // 使用convert方法将关注关系列表转换为AuthorInterestDTO列表
        IPage<AuthorInterestDTO> interestAuthorsPage = interestRelationsPage.convert(interestDO -> {
            // 获取作者DO对象
            AuthorDO authorDO = baseMapper.selectById(interestDO.getAuthorId());
            // 创建AuthorInterestDTO对象并设置相关信息
            AuthorInterestDTO authorInterestDTO = new AuthorInterestDTO();
            authorInterestDTO.setAuthor_id(interestDO.getAuthorId());
            authorInterestDTO.setAuthor_name(authorDO.getAuthorName());
            authorInterestDTO.setInterest_time(interestDO.getInterestTime());

            return authorInterestDTO;
        });

        return interestAuthorsPage;
    }

    @Override
    public List<AuthorDTO> getAuthorDTOsByBathId(List<Integer> authorIds) {
        AuthorConverter converter = AuthorConverter.INSTANCE;
        return baseMapper.selectBatchIds(authorIds)
                .stream().map(converter::authorDoToDto).collect(Collectors.toList());
    }

    @Override
    public AuthorInfoDTO getAuthorInfoById(Integer authorId) {

        AuthorInfoDTO authorInfoDTO = new AuthorInfoDTO();
        // 获取作者
        String authorName=baseMapper.selectById(authorId).getAuthorName();
//        authorInfoDTO.setAuthor_name(authorName);
        // 根据作者id得到他参与的所有论文id
        // 得到论文
        return null;
    }

//    @Override
//    public List<AuthorDTO> getAuthorDTOsByPaperId(int paperId) {
//        return null;
//    }
}
